﻿using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.Bill;
using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DzInfo.XH.FormPlugIn
{
    [Description("茂名新辉收款单表单插件"), HotUpdate]
    public class RECEIVEBILL : AbstractBillPlugIn
    {
        public override void DataChanged(DataChangedEventArgs e)
        {
            base.DataChanged(e);
            if (e.Key == "FPAYUNIT")
            {
                if (BindData()) { return; }
                string PAYUNITTYPE = this.Model.DataObject["PAYUNITTYPE"].ToString();
                if (PAYUNITTYPE == "BD_Customer")
                {
                    long custId = Convert.ToInt64(e.NewValue);
                    string strDate = DateTime.Now.ToString("yyyy-MM-dd");
                    string endDate = DateTime.Now.ToString("yyyy-MM-dd");
                    decimal allDatas = GetDatas(custId, strDate, endDate);
                    if (allDatas != 0)
                    {
                        this.Model.SetValue("FDQZQK", allDatas, 0);
                    }
                }
            }
        }

        private bool BindData()
        {
            
            return false;
        }

        private decimal GetDatas(long custId, string strDate, string endDate)
        {
            string sql = string.Format($@"/*dialect*/ 
            SELECT
                ISNULL(A1.FQCYS, 0) - ISNULL(A2.FQCSK, 0) AS FQCYE
            FROM (
                SELECT 
                    T0.FCUSTOMERID AS FCUSTID,
                    SUM(T1.FALLAMOUNT) AS FQCYS
                FROM t_AR_receivable T0
                JOIN t_AR_receivableEntry T1 ON T1.FID = T0.FID
                WHERE T0.FCUSTOMERID={custId}
                GROUP BY T0.FCUSTOMERID
            ) A1
            FULL JOIN (
                SELECT 
                    T0.FCONTACTUNIT AS FCUSTID,
                    SUM(T1.FREALRECAMOUNT) AS FQCSK
                FROM T_AR_RECEIVEBILL T0
                JOIN T_AR_RECEIVEBILLENTRY T1 ON T1.FID = T0.FID
                WHERE T0.FCONTACTUNITTYPE = 'BD_Customer'
                    AND T0.FCONTACTUNIT={custId}
                GROUP BY T0.FCONTACTUNIT
            ) A2 ON A2.FCUSTID = A1.FCUSTID ");
            DynamicObjectCollection doc = DBUtils.ExecuteDynamicObject(this.Context, sql);
            if (doc.Count > 0)
            {
                return Convert.ToDecimal(doc[0][0]);
            }
            else
            {
                return 0;
            }
        }
        public override void ButtonClick(ButtonClickEventArgs e)
        {
            base.ButtonClick(e);
            if (e.Key.Equals("FKHDZ"))
            {
                if (BindData()) { return; }
                string PAYUNITTYPE = this.Model.DataObject["PAYUNITTYPE"].ToString();
                if (PAYUNITTYPE == "BD_Customer")
                {
                    var cust = this.Model.DataObject["PAYUNIT"] as DynamicObject;
                    string custId = cust == null ? "0" : cust["id"].ToString();
                    DynamicObject org = this.Model.DataObject["FPAYORGID"] as DynamicObject;
                    string orgId = org == null ? "0" : org["id"].ToString();
                    BillShowParameter showParam = new BillShowParameter();
                    showParam.FormId = "POSW_KHWLZZB";
                    //showParam.PKey = "100462";
                    //传递自定义参数
                    showParam.CustomParams.Add("CustId", custId);
                    showParam.CustomParams.Add("OrgId", orgId);
                    if (custId != "0")
                    {
                        this.View.ShowForm(showParam);
                    }
                    else
                    {
                        this.View.ShowErrMessage("客户为空，请先选择客户！");
                        return;
                    }
                }
            }
        }
    }
}
